package com.nativetalk.controller.app.v4_3_1;

import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.member.TdNtMember;
import com.nativetalk.bean.member.TdNtMemberDetail;
import com.nativetalk.bean.teacher.TdNtTeacherResult;
import com.nativetalk.common.Constants;
import com.nativetalk.common.Lang;
import com.nativetalk.common.util.JsonUtil;
import com.nativetalk.common.util.MemcachedUtils;
import com.nativetalk.service.member.search.MemberSearchService;
import com.nativetalk.service.member.update.MemberUpdateService;
import com.nativetalk.service.teacher.search.TeacherSearchService;
import com.nativetalk.service.teacher.update.TeacherUpdateService;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Peter on 2017/7/7.
 */
@Controller
public class JustTalkController {

    private static Logger logger = Logger.getLogger(JustTalkController.class);

    @Autowired
    private MemberUpdateService memberUpdateService;

    @Autowired
    private MemberSearchService memberSearchService;

    @Autowired
    private TeacherUpdateService teacherUpdateService;

    @Autowired
    private TeacherSearchService teacherSearchService;

    /**
     * @return RetInfo
     * @Purpose 添加或更新JustTalk User Uri
     * @version 4.3.1
     * @time 2017-07-07
     * @author Peter
     */
    @RequestMapping(value = "/app/14/token/justtalk/useruri/update", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo updateMemberJustTalkUserUri(@RequestBody Map<String, String> map,
                                               @RequestHeader(value = "token", required = false) String token) {
        try {
            //校验token，如果校验通过就从缓存中读取Member
            if (StringUtils.isBlank(token)) {
                RetInfo retInfo = RetInfo.getClientErrorInfo("Parameter token is required");
                logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
                return retInfo;
            }
            Object o = MemcachedUtils.get(token);
            if (o == null) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存中没有MemberToken:[" + token + "]，用户需要重新登录系统。");
                logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
                return retInfo;
            }

            if (!TdNtMember.class.isInstance(o)) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.ZH);
                logger.info("缓存的token不是TdNtMember类型:[" + token + "]，用户需要重新登录系统。");
                logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
                return retInfo;
            }
            TdNtMember tdNtMember = (TdNtMember) o;

            //校验参数
            logger.info(Constants.prefix_in + JsonUtil.toJson(map));
            Object obj_justTalkUserUri = map.get("justTalkUserUri");
            if (obj_justTalkUserUri == null) {
                RetInfo retInfo = RetInfo.getClientErrorInfo("justTalkUserUri can not be null.");
                //打印出参
                logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
                return retInfo;
            }
            String justTalkUserUri = (String) obj_justTalkUserUri;
            if (StringUtils.isBlank(justTalkUserUri)) {
                RetInfo retInfo = RetInfo.getClientErrorInfo("justTalkUserUri can not be blank.");
                //打印出参
                logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
                return retInfo;
            }

            memberUpdateService.updateMemberJustTalkUserUri(tdNtMember.getMemb_id(), justTalkUserUri);
            RetInfo retInfo = RetInfo.getSuccessInfo();

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo(Lang.ZH);
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 查询学生的JustTalk User Uri
     * @version 4.3.1
     * @time 2017-07-07
     * @author Peter
     */
    @RequestMapping(value = "/app/14/member/justtalk/useruri/find", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo findMemberJustTalkUserUri(@RequestBody Map<String, Integer> map) {
        try {
            //校验参数
            logger.info(Constants.prefix_in + JsonUtil.toJson(map));
            Integer memb_id = map.get("memb_id");
            TdNtMemberDetail memberDetail = memberSearchService.findTdNtMemberDetail(memb_id);
            if (memberDetail == null) {
                RetInfo retInfo = RetInfo.getClientErrorInfo("The member is not exists(memb_id=" + memb_id + ").");
                logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
                return retInfo;
            }

            RetInfo retInfo = RetInfo.getSuccessInfo();
            Map<String, String> obj = new HashMap<>();
            obj.put("justTalkUserUri", memberDetail.getJusttalk_user_uri());
            retInfo.setObj(obj);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo(Lang.ZH);
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 新增或更新老师的JustTalk User Uri
     * @version 4.3.1
     * @time 2017-07-07
     * @author Peter
     */
    @RequestMapping(value = "/app/14/teatoken/justtalk/useruri/update", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo updateTeacherJustTalkUserUri(@RequestBody Map<String, String> map,
                                                @RequestHeader(value = "token", required = false) String token) {
        try {
            //校验token，如果校验通过就从缓存中读取Teacher
            if (StringUtils.isBlank(token)) {
                RetInfo retInfo = RetInfo.getClientErrorInfo("token is required");
                logger.info(JsonUtil.toJson(retInfo));
                return retInfo;
            }
            Object o = MemcachedUtils.get(token);
            if (o == null) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.EN);
                logger.info("缓存中没有teatoken:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }

            if (!TdNtTeacherResult.class.isInstance(o)) {
                RetInfo retInfo = RetInfo.getReloginInfo(Lang.EN);
                logger.info("缓存的token不是TdNtTeacherResult类型:[" + token + "]，用户需要重新登录系统。 retInfo-->" + JsonUtil.toJson(retInfo));
                return retInfo;
            }
            TdNtTeacherResult teacher = (TdNtTeacherResult) o;

            //校验参数
            logger.info(Constants.prefix_in + JsonUtil.toJson(map));
            Object obj_justTalkUserUri = map.get("justTalkUserUri");
            if (obj_justTalkUserUri == null) {
                RetInfo retInfo = RetInfo.getClientErrorInfo("justTalkUserUri can not be null.");
                //打印出参
                logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
                return retInfo;
            }
            String justTalkUserUri = (String) obj_justTalkUserUri;
            if (StringUtils.isBlank(justTalkUserUri)) {
                RetInfo retInfo = RetInfo.getClientErrorInfo("justTalkUserUri can not be blank.");
                //打印出参
                logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
                return retInfo;
            }

            teacherUpdateService.updateTeacherJustTalkUserUri(teacher.getTea_id(), justTalkUserUri);
            RetInfo retInfo = RetInfo.getSuccessInfo();

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 查询老师的JustTalk User Uri
     * @version 4.3.1
     * @time 2017-07-07
     * @author Peter
     */
    @RequestMapping(value = "/app/14/teacher/justtalk/useruri/find", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo findTeacherJustTalkUserUri(@RequestBody Map<String, Integer> map) {
        try {
            //校验参数
            logger.info(Constants.prefix_in + JsonUtil.toJson(map));
            Integer tea_id = map.get("tea_id");
            TdNtTeacherResult teacher = teacherSearchService.findByTeaId(tea_id);
            if (teacher == null) {
                RetInfo retInfo = RetInfo.getClientErrorInfo("老师不存在，tea_id=" + tea_id);
                logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
                return retInfo;
            }

            RetInfo retInfo = RetInfo.getSuccessInfo();
            Map<String, String> obj = new HashMap<>();
            obj.put("justTalkUserUri", teacher.getJusttalk_user_uri());
            retInfo.setObj(obj);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }


}
